package com.water.gateway.security;

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;

@Configuration
public class GatewayConfig {
//	@Bean
//	public OAuth2AuthorizedClientManager authorizedClientManager(
//	        ClientRegistrationRepository clientRegistrationRepository,
//	        OAuth2AuthorizedClientService authorizedClientService) {
//	    OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
//	            .authorizationCode()
//	            .refreshToken() // 关键：开启 refresh_token 支持
//	            .build();
//
//	    AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager =
//	            new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientService);
//	    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
//
//	    return authorizedClientManager;
//	}
	
	//@Bean
	public GlobalFilter corsFilter() {
	    return (exchange, chain) -> {
	        ServerHttpRequest request = exchange.getRequest();
	        if (request.getMethod() == HttpMethod.OPTIONS) {
	            ServerHttpResponse response = exchange.getResponse();
	            response.setStatusCode(HttpStatus.OK);
	            response.getHeaders().add("Access-Control-Allow-Origin", "*");
	            response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	            response.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Requested-With");
	            return response.setComplete();
	        }
	        return chain.filter(exchange);
	    };
	}
}
